﻿<?php
ini_set('date.timezone','Asia/Shanghai');
include 'simple_html_dom.php';
include 'config_catchanzhi.php';
include 'function_catchanzhi_all.php';

define('APPS_NUM',2405);
define('GAMES_NUM',977);

define('DBPAS',"coolchuan2012");
//define('DBPAS',"123456");
define('LOGPATHHEADER','/var/www/catchjiankongdata/');
//define('LOGPATHHEADER','');

$link = mysql_connect("localhost","root",DBPAS); 
//$link = mysql_connect("localhost","root","coolchuan2012"); 
mysql_select_db("catchappdataall", $link);
mysql_query('SET NAMES UTF8');


define('ONCECATCHCOUNT',15);
define('ONCECOMMENTCATCHCOUNT',50);

define('WEBURLHEADER','http://www.anzhi.com/soft_');
define('WAPURLHEADER','http://m.anzhi.com');

define('WEBURLHEADERANZHI','http://www.anzhi.com/soft_');
define('WAPURLHEADERANZHI','http://m.anzhi.com/info_');



//$apps_url_page_header = "http://openbox.mobilem.360.cn/AppStore/getRecomendAppsBytype?type=1&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540&startCount=21&start=0&count=".ONCECATCHCOUNT;
//$apps_url_app_all_list = "http://openboxcdn.mobilem.360.cn/AppStore/getTopapplist?type=1&os=17&model=HTC+Sensation&cpu=pyramid&&screen_size=960x540&order=down&start=0&count=".ONCECATCHCOUNT;
define('LOGFORSUCCESS',0);


getinfoandrank();

//getcomment();


function getinfoandrank()
{
	//获取已存在app基础数据
/*	
	//应用分类
	$apptype_arr = getapptypelist();
	foreach ($apptype_arr as $apptype)
	{
		//每一类的url
		$apps_url_app_type_list = $apptype['url'];
		decodeurlandgetdata($apptype['id'],"soft",$apps_url_app_type_list);
	}
	//应用总榜
	decodeurlandgetdata(101,"soft","http://m.anzhi.com/recommend_1_");
*/	
	
	//游戏分类
	$gametype_arr = getgametypelist();
//	foreach ($gametype_arr as $gametype)
//	{
//		if(($gametype['id']>=51) && ($gametype['id']<=51))
//		{
//			//每一类的url
//			$apps_url_game_type_list = $gametype['url'];
//			decodeurlandgetdata($gametype['id'],"game",$apps_url_game_type_list);
//		}
//	}
	//游戏总榜
	decodeurlandgetdata(102,"game","http://m.anzhi.com/recommend_2_");
	catchother();
}



function decodeurlandgetdata($catchtype,$appbigtype,$urlheader)
{
	$app_exit_infos = get_exit_appinfo_fromdb();
	$app_exit_versions = get_exit_appversion_fromdb();
	
	$app_today_data = get_today_data();
	$app_today_score = get_today_score();
	
	for($i=0;$i<=1000;)
	{
		
		$apps_url_app_all_list = $urlheader.$i.".html";
		
//		本次为空，结束
//		if((count($appinfoarr) == 0) || ($appinfoarr == ""))
//		{
//			break;
//		}
		
		//有内容，解析
		
		$rankpos = ($i) * ONCECATCHCOUNT;
		$app_info_arr_insertdb_arr = catchinfodata($catchtype,$appbigtype,$rankpos,$apps_url_app_all_list,$i);
		
		if((count($app_info_arr_insertdb_arr) == 0) || ($app_info_arr_insertdb_arr == ""))
		{
			break;
		}
		
//		var_dump($app_info_arr_insertdb_arr);
//		continue;

		$app_info_arr_insertdb_data = array();
		$app_info_arr_insertdb_scoredata = array();
		//写数据库
		foreach ($app_info_arr_insertdb_arr as $app_info_arr_insertdb)
		{
			//判断是否需要更新信息库
			$searchinfoid = searchisexit($app_info_arr_insertdb, $app_exit_infos);
			if($searchinfoid == 0)
			{
				$app_info_arr_insertdb['app_id'] = insert_appinfo_ifnotexit_todb($app_info_arr_insertdb);
				$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
				
				//重新获取存在app信息，因为有了更新
				$app_exit_infos = get_exit_appinfo_fromdb();
				$app_exit_versions = get_exit_appversion_fromdb();
			}
			else 
			{
				$app_info_arr_insertdb['app_id'] = $searchinfoid;
				$searchversionid = searchisexitversion($app_info_arr_insertdb, $app_exit_versions);
				if($searchversionid == 0)
				{
					$app_info_arr_insertdb['app_version_id'] = insert_appversion_ifnotexit_todb($app_info_arr_insertdb);
					
					//重新获取存在app信息，因为有了更新
					$app_exit_versions = get_exit_appversion_fromdb();
				}
				else 
				{
					$app_info_arr_insertdb['app_version_id'] = $searchversionid;
				}
			}
			
			//判断当天没有抓过，再写入
			if(searchisexittodaydata($app_info_arr_insertdb, $app_today_data) == 0)
			{
				//update_appinfo_todb($app_info_arr_insertdb);
				//$app_today_data = get_today_data();
				$app_info_arr_insertdb_data[] = $app_info_arr_insertdb;
			}
			if(searchisexittodayscore($app_info_arr_insertdb, $app_today_score) == 0)
			{
				//update_appscore_todb($app_info_arr_insertdb);
				//$app_today_score = get_today_score();
				$app_info_arr_insertdb_scoredata[] = $app_info_arr_insertdb;
			}
		}
		
		update_appinfo_todb($app_info_arr_insertdb_data);
		update_appscore_todb($app_info_arr_insertdb_scoredata);
//		$app_today_data = get_today_data();
//		$app_today_score = get_today_score();
		$app_info_arr_insertdb_data = array();
		$app_info_arr_insertdb_scoredata = array();
		
		$i++;
		
	//	结束
	//	if($obj->total < ONCECATCHCOUNT)
	//	{
	//		break;
	//	}
	}
}



function get_exit_appinfo_fromdb_forcatch()
{
	$link = mysql_connect("localhost","root",DBPAS); 
	$createtime = strtotime(date("Y-m-d H:i:s"));

	$sql = 'select id, app_id, packageName,title,apptype,appbigtype from jk_apps_info_anzhi where apptype =102 and IsDeleted=0 '; 
	$rs = mysql_query($sql, $link);
	
	$app_exit_info = array();
  	while($row=mysql_fetch_array($rs))        
  	{
  		$app_exit_info_temp = array();
  		$app_exit_info_temp['id'] = $row['id'];
  		$app_exit_info_temp['app_id'] = $row['app_id'];
	  	$app_exit_info_temp['AppPackageName'] = $row['packageName'];
	  	$app_exit_info_temp['AppName'] = $row['title'];
	  	$app_exit_info_temp['apptype'] = $row['apptype'];
	  	$app_exit_info_temp['appbigtype'] = $row['appbigtype'];
	  	$app_exit_info[] = $app_exit_info_temp;
  	}
	return $app_exit_info;
}

?>